package com.keon.jianbao.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import com.keon.jianbao.R;
import com.keon.jianbao.adapter.JianBaoPayListAdapter;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.bean.Appraisal;
import com.keon.jianbao.bean.Expert;
import com.keon.jianbao.ui.widget.ItemLayout;
import com.keon.jianbao.ui.widget.NoScrollListView;
import com.keon.jianbao.util.StringUtils;

import java.util.ArrayList;

import butterknife.Bind;

/**
 * Created by keon on 2015/10/8.
 */
public class JianBaoPayActivity extends BaseActivity {

    Expert expert = null;
    ArrayList<Appraisal> appraisalList = null;
    Appraisal appraisal = null;

    JianBaoPayListAdapter adapter;

    @Bind(R.id.pay_expert)
    ItemLayout ilExpert;
    @Bind(R.id.pay_list)
    NoScrollListView nslvList;
    @Bind(R.id.pay_add)
    TextView tvAdd;
    @Bind(R.id.pay_edit)
    TextView tvEdit;
    @Bind(R.id.pay_text)
    TextView tvText;
    @Bind(R.id.pay_submit)
    Button bSubmit;
    @Bind(R.id.pay_agreement)
    CheckBox cbAgreement;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_jianbao_pay;
    }

    @Override
    protected int getToolBarTitle() {
        return R.string.jianbao_pay;
    }

    @Override
    public void initView() {
        tvEdit.setOnClickListener(this);
        tvAdd.setOnClickListener(this);
        bSubmit.setOnClickListener(this);
    }

    @Override
    public void initData() {
        initJianBaoList(getIntent());

        if (expert != null) {
            ilExpert.setTitle(R.string.jianbao_pay_expert, expert.getExpert_name());
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.pay_add:
                Bundle bundle = getIntent().getExtras();
                jump(UploadJianBaoActivity.class, bundle);
                break;
            case R.id.pay_edit:
                if (getString(R.string.jianbao_pay_edit) == tvEdit.getText().toString()) {
                    adapter.showDelBtn(true);
                    tvEdit.setText(R.string.jianbao_pay_edit_done);
                } else if (getString(R.string.jianbao_pay_edit_done) == tvEdit.getText().toString()) {
                    adapter.showDelBtn(false);
                    tvEdit.setText(R.string.jianbao_pay_edit);
                }
                break;
            case R.id.pay_submit:
                if (cbAgreement.isChecked()) {
                    Bundle bundle1 = new Bundle();
                    bundle1.putSerializable("app", appraisal);
                    bundle1.putDouble("amount", expert.getExpert_fee());
                    jump(JianBaoPayWayActivity.class, bundle1);
                    break;
                } else {
                    showToast(R.string.jianbao_pay_agreement_tip);
                }

        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        initJianBaoList(intent);
    }

    void initJianBaoList(Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            expert = (Expert) bundle.getSerializable("expert");
            appraisal = (Appraisal) bundle.getSerializable("appraisal");
        }
        if (appraisal != null) {
            if (appraisalList == null || appraisalList.size() == 0) {
                appraisalList = new ArrayList<>();
                appraisalList.add(appraisal);
                adapter = new JianBaoPayListAdapter(appraisalList);
                nslvList.setAdapter(adapter);
            } else {
                appraisalList.add(appraisal);
                adapter.notifyDataSetChanged();
            }
        }
        initText();
    }

    void initText() {
        String text = String.format(getString(R.string.jianbao_pay_text), appraisalList.size(), StringUtils.getDecimal(expert.getExpert_fee(), 2));
        tvText.setText(text);
    }
}
